import React, { PureComponent } from 'react';
import { connect } from 'dva';
import {
  Row,
  Col,
  Card,
  Form,
  Input,
  Button,
  Table,
} from 'antd';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';

import styles from './CodeEarnubgsList.less';

const FormItem = Form.Item;

const getValue = obj =>
  Object.keys(obj)
    .map(key => obj[key])
    .join(',');


@connect(({ rule, loading }) => ({
  rule,
  loading: loading.models.rule,
}))
@Form.create()
export default class TableList extends PureComponent {
  state = {
    formValues: {},
    actId:null,
    cId:null,
    cCode:null,
    desc:null,
    pageNum:0,
  };

  componentDidMount() {
    const { dispatch } = this.props;
    const { actId,cId,cCode,desc,pageNum } = this.state;
    dispatch({
      type: 'rule/getPromoCodeRewardList',
      payload: { actId,cId,cCode,desc,pageNum },
    });
  }


  handleStandardTableChange = (pagination, filtersArg, sorter) => {
    const { dispatch } = this.props;
    const { formValues,actId,cId,cCode,desc } = this.state;

    const filters = Object.keys(filtersArg).reduce((obj, key) => {
      const newObj = { ...obj };
      newObj[key] = getValue(filtersArg[key]);
      return newObj;
    }, {});

    this.setState({
      pageNum : pagination.current-1,
    });
    const params = {
      pageNum: pagination.current-1,
      pageSize: pagination.pageSize,
      ...formValues,
      ...filters,
      actId,
      cId,
      cCode,
      desc,
    };
    if (sorter.field) {
      params.sorter = `${sorter.field}_${sorter.order}`;
    }

    dispatch({
      type: 'rule/getPromoCodeRewardList',
      payload: params,
    });
  };

  handleFormReset = () => {
    const { form, dispatch } = this.props;
    form.resetFields();
    this.setState({
      formValues: {},
      actId:null,
      cId:null,
      cCode:null,
      desc:null,
      pageNum:0,
    });
    dispatch({
      type: 'rule/getPromoCodeRewardList',
      payload: {
        actId:null,
        cId:null,
        cCode:null,
        desc:null,
        pageNum:0,
      },
    });
  };



  handleSearch = e => {
    e.preventDefault();

    const { dispatch, form } = this.props;

    form.validateFields((err, fieldsValue) => {
      if (err) return;

      this.setState({
        actId: fieldsValue.actId,
        cId: fieldsValue.cId,
        cCode: fieldsValue.cCode,
        desc: fieldsValue.desc,
      });
      const { pageNum } = this.state;
      dispatch({
        type: 'rule/getPromoCodeRewardList',
        payload: { actId:fieldsValue.actId,cId:fieldsValue.cId,cCode:fieldsValue.cCode,desc:fieldsValue.desc,pageNum },
      });
    });
  };


  // 筛选条件
  renderSimpleForm() {
    const { form } = this.props;
    const { getFieldDecorator } = form;
    return (
      <Form onSubmit={this.handleSearch} layout="inline">
        <Row gutter={{ md: 8, lg: 24, xl: 48 }}>
          <Col md={6} sm={24}>
            <FormItem label="活动id">
              {getFieldDecorator('actId')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>
          <Col md={6} sm={24}>
            <FormItem label="邀请码id">
              {getFieldDecorator('cId')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>
          <Col md={6} sm={24}>
            <FormItem label="邀请码编号">
              {getFieldDecorator('cCode')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>
          <Col md={6} sm={24}>
            <FormItem label="邀请码描述">
              {getFieldDecorator('desc')(<Input placeholder="请输入" />)}
            </FormItem>
          </Col>
          <div style={{ overflow: 'hidden' }}>
            <span style={{ float: 'right', marginBottom: 24 }}>
              <Button type="primary" htmlType="submit">
                查询
              </Button>
              <Button style={{ marginLeft: 8 }} onClick={this.handleFormReset}>
                重置
              </Button>
            </span>
          </div>
        </Row>
      </Form>
    );
  }

  render() {
    const {
      rule: { data },
      loading,
    } = this.props;

    const paginationProps = {
      showSizeChanger: true,
      showQuickJumper: true,
      ...data.pagination,
    };

    const columns = [
      {
        title: '活动id',
        dataIndex: 'act_id',
      },
      {
        title: '活动标题',
        dataIndex: 'act_name',
      },
      {
        title: '邀请码id',
        dataIndex: 'c_id',
      },
      {
        title: '邀请码编号',
        dataIndex: 'c_code',
      },
      {
        title: '备注',
        dataIndex: 'desc',
      },
      {
        title: '总推荐人数',
        dataIndex: 'p_total_cnt',
      },
      {
        title: '有效人数',
        dataIndex: 'p_valid_cnt',
      },
      {
        title: '推荐奖励',
        dataIndex: 'p_reward',
      },
    ];


    return (
      <PageHeaderLayout title="邀请码收益">
        <Card bordered={false}>
          <div className={styles.tableList}>
            <div className={styles.tableListForm}>{this.renderSimpleForm()}</div>
            <Table
              loading={loading}
              dataSource={data.list}
              columns={columns}
              pagination={paginationProps}
              onChange={this.handleStandardTableChange}
            />
          </div>
        </Card>
      </PageHeaderLayout>
    );
  }
}
